<template>
  <div class="login-page">
    <van-nav-bar
  title="账号登录"
  left-arrow
  @click-left="$router.push('/my')"
/>
    <van-form @submit="onSubmit" validate-trigger="onSubmit">
  <van-field
    v-model="username"
    name="username"
    label=""
    placeholder="请输入账号"

    :rules="[{ required: true, message: '用户名不能为空' },
    { pattern: /^\w{6,}$/, message: '用户名至少包含6个字符' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    label=""
    placeholder="请输入密码"

    :rules="[{ required: true, message: '密码不能为空' },
    { pattern: /^\w{6,}$/, message: '密码至少包含6个字符' }
    ]"
  />
  <div style="margin: 16px;">
    <van-button color="#21b97a" block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>
<router-link class="link" to="/register">没账号？去注册~</router-link>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      const res = await login(values)
      console.log(res)
      setToken(res.body.token)
      this.$toast.success(res.description)
      this.$router.push('/home')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  display: block;
  display: flex;
  justify-content: center;
 margin-top: 30px ;
  color: #666;
  font-size: 14px;
  padding-right: 20px;
  
}
</style>
